package com.whfc.wxmp.api.quality.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.*;
import com.whfc.common.util.DateUtil;
import com.whfc.wxmp.annotation.CurrentWxUser;
import com.whfc.entity.param.check.CheckSearchParam;
import com.whfc.fuum.entity.WxUser;
import com.whfc.quality.dto.QualityTaskExecDTO;
import com.whfc.quality.param.QuailTaskCompleteParam;
import com.whfc.quality.param.QualityTaskReportParam;
import com.whfc.wxmp.api.quality.service.WxQualityTaskService;
import com.whfc.wxmp.config.AbstractBaseController;
import com.whfc.wxmp.util.RequestConetxtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collections;
import java.util.Date;

/**
 * @Description 检查任务
 * @Author hw
 * @Date 2021-05-18 9:59
 * @Version 1.0
 */
@RestController
@RequestMapping("/mp/api/quality/task/")
public class WxQualityTaskController extends AbstractBaseController {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private WxQualityTaskService wxQualityTaskService;

    @GetMapping(value = "list", params = {"pageSize", "pageNum"})
    public Result list(@CurrentWxUser WxUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "report", required = false) Integer report,
                       @RequestParam(value = "dayTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date dayTime,
                       @RequestParam(value = "monthTime", required = false) @DateTimeFormat(pattern = "yyyy-MM") Date monthTime,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("pageNum") Integer pageNum) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "检查任务列表(分页)", RequestConetxtUtil.getParameters());
        Date startTime = null;
        Date endTime = null;
        if (dayTime != null) {
            startTime = DateUtil.getDateBegin(dayTime);
            endTime = DateUtil.getDateEnd(dayTime);
        } else if (monthTime != null) {
            startTime = DateUtil.getMonthBegin(monthTime);
            endTime = DateUtil.getMonthEnd(monthTime);
        }
        PageData<QualityTaskExecDTO> list = wxQualityTaskService.list(user.getPhone(), deptId, report, startTime, endTime, pageSize, pageNum);
        return ResultUtil.success(list);
    }

    @GetMapping("list")
    public Result list(@CurrentWxUser WxUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "dayTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date dayTime,
                       @RequestParam(value = "monthTime", required = false) @DateTimeFormat(pattern = "yyyy-MM") Date monthTime,
                       @RequestParam(value = "report", required = false) Integer report) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "检查任务列表(不分页)", RequestConetxtUtil.getParameters());
        Date startTime = null;
        Date endTime = null;
        if (dayTime != null) {
            startTime = DateUtil.getDateBegin(dayTime);
            endTime = DateUtil.getDateEnd(dayTime);
        } else if (monthTime != null) {
            startTime = DateUtil.getMonthBegin(monthTime);
            endTime = DateUtil.getMonthEnd(monthTime);
        }
        ListData<QualityTaskExecDTO> list = wxQualityTaskService.list(user.getPhone(), deptId, report, startTime, endTime);
        return ResultUtil.success(list);
    }

    @PostMapping("/exec/add")
    public Result execAdd(@CurrentWxUser WxUser user,
                          @Validated @RequestBody QualityTaskReportParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "排查上报", param);

        wxQualityTaskService.execAdd(param, user.getPhone());
        return ResultUtil.success();
    }

    @PostMapping("/complete")
    public Result execComplete(@CurrentWxUser WxUser user,
                               @Validated @RequestBody QuailTaskCompleteParam param) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "完成任务", RequestConetxtUtil.getParameters());
        wxQualityTaskService.execComplete(param);
        return ResultUtil.success();
    }

    @GetMapping("/meList")
    public Result meList(@CurrentWxUser WxUser user,
                         @RequestParam("deptId") Integer deptId,
                         @RequestParam("pageNum") Integer pageNum,
                         @RequestParam("pageSize") Integer pageSize) {
        logger.info("WXMPUA|{}|{}|{}|{}|{}",
                user.getId(), RequestConetxtUtil.getAppType(), RequestConetxtUtil.getPage(),
                "我的任务", RequestConetxtUtil.getParameters());
        PageData<QualityTaskExecDTO> list = wxQualityTaskService.meList(user.getPhone(), deptId, pageSize, pageNum);
        return ResultUtil.success(list);
    }

    /**
     * 2.0.0巡查问题分页
     *
     * @param param
     * @return
     */
    @PostMapping("/page")
    public R<PageVO<QualityTaskExecDTO>> page(@Valid @RequestBody CheckSearchParam param) {
        logger.info("巡查问题分页:param={},wxUserId={}", param, getLoginUser(request).getId());
        param.setStateList(Collections.singletonList(2));
        PageVO<QualityTaskExecDTO> res = wxQualityTaskService.page(param, getLoginUser(request).getPhone());
        return R.success(res);
    }
}
